package com.example.sbbsec.controller;

import com.example.sbbsec.common.Constant;
import com.example.sbbsec.domain.View;
import com.example.sbbsec.domain.stat.common.ExamRegistration;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.data.util.CloseableIterator;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.google.common.collect.Lists.newArrayList;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;

/**
 * @author Arat.Wu
 * @date 2018/11/16
 */
@Controller
public class IndexController {
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @RequestMapping(value = "/index")
    public String index() {
        boolean b = elasticsearchTemplate.indexExists("syzkmis-view");
        boolean bb = elasticsearchTemplate.indexExists("syzkmis-view");

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices(Constant.V_EXAM_INDEX)
                .withPageable(PageRequest.of(0, 3000))
                .build();
        SearchQuery searchQueryView = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices("info")
                .build();
        final List<String> strings = elasticsearchTemplate.queryForIds(searchQuery);

        final String query1 = elasticsearchTemplate.query(searchQuery, response -> {
            //直接返回es原封结果到前端
            return response.toString();
        });
        System.out.println(query1);

        ArrayList<String> arrayList = new ArrayList<>();

       // QueryBuilder queryBuilder = QueryBuilders.matchQuery("name","吴亦凡").analyzer("ik_max_word");
        QueryBuilder queryBuilder1 = QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("name.keyword","周倩"));
        QueryBuilder queryBuilder2 = QueryBuilders.matchPhraseQuery("name","周倩");//.analyzer("ik_max_word");

        SearchQuery searchQuery1 = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder1)
                .withIndices(Constant.V_EXAM_INDEX)
                .withSort(new FieldSortBuilder("id").order(SortOrder.DESC))
                .withPageable(PageRequest.of(0, 3000))
                .withIds(arrayList)
                .build();
        SearchQuery searchQuery2 = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder2)
                .withIndices(Constant.V_EXAM_INDEX)
                .withSort(new FieldSortBuilder("id").order(SortOrder.DESC))
                .withIds(arrayList)
                .build();

        final List<String> strings1 = elasticsearchTemplate.queryForIds(searchQuery1);

        final List<String> strings2 = elasticsearchTemplate.queryForIds(searchQuery2);

        CriteriaQuery query = new CriteriaQuery(
                new Criteria("_all").contains("吴亦凡"));
        //final List<String> strings2 = elasticsearchTemplate.queryForList(searchQuery2);

        SearchHits hits1 = elasticsearchTemplate.query(searchQuery1, response ->  response.getHits());
        SearchHits hits2 = elasticsearchTemplate.query(searchQuery, response ->  response.getHits());
        SearchHit[] hits = hits2.getHits();
        SearchHit[] searchHits = Arrays.copyOf(hits, hits.length);
        List<SearchHit> ExamRegistrationList =Arrays.stream(hits2.getHits()).collect(Collectors.toList());
        List<Map> strings3=newArrayList();
        ExamRegistrationList.forEach(item->{
            strings3.add(item.getSourceAsMap());
        });

        List<ExamRegistration> examRegistrationList = elasticsearchTemplate.queryForList(searchQuery,ExamRegistration.class);
        //List<View> ViewList = elasticsearchTemplate.queryForList(searchQueryView,View.class);

        return "/view/index";
    }

    @RequestMapping(value = "/index/info")
    @ResponseBody
    public List<Map>  index2() {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices(Constant.V_EXAM_INDEX)
                .withPageable(PageRequest.of(0, 10000))
                .build();

        SearchHits hits2 = elasticsearchTemplate.query(searchQuery, response ->  response.getHits());
        SearchHit[] hits = hits2.getHits();
        SearchHit[] searchHits = Arrays.copyOf(hits, hits.length);
        List<SearchHit> ExamRegistrationList =Arrays.stream(hits2.getHits()).collect(Collectors.toList());
        List<Map> strings3=newArrayList();
        ExamRegistrationList.forEach(item->{
            strings3.add(item.getSourceAsMap());
        });
        //List<ExamRegistration> examRegistrationList = elasticsearchTemplate.queryForList(searchQuery,ExamRegistration.class);
        //return
        return strings3;
    }

    @RequestMapping(value = "/index/info3")
    @ResponseBody
    public SearchHit[]  index3() {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices(Constant.V_EXAM_INDEX)
                .withPageable(PageRequest.of(0, 10000))
                .build();

        SearchHits hits2 = elasticsearchTemplate.query(searchQuery, response ->  response.getHits());
        return hits2.getHits();
    }

    @RequestMapping(value = "/index/info4")
    @ResponseBody
    public List<View> index4() {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(matchAllQuery())
                .withIndices(Constant.V_EXAM_INDEX)
                .withPageable(PageRequest.of(0, 10000))
                .build();
        CloseableIterator<View> stream= elasticsearchTemplate.stream(searchQuery,View.class);
        List<View> sampleEntities = new ArrayList<>();
        while (stream.hasNext()) {
            sampleEntities.add(stream.next());
        }
        return sampleEntities;
    }
}
